﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.IO;
using System.Xml.Serialization;
using Common;

namespace Database
{
    /// <summary>
    /// 任务数据库
    /// </summary>
    public class TaskDatabase
    {
        private static Task Task以牙还牙以眼还眼 = new Task("以牙还牙以眼还眼",new byte[]{0xA4,0x12,0x01,0x00},"风木园","风木鬼族");

        private static Dictionary<string, Task> taskDict = new Dictionary<string, Task>(256);
        private static List<Task> taskList = null;

        public static void Init()
        {
            Console.WriteLine("开始加载任务数据库...");
            Load();
            if (taskList == null)
            {
                #region 程序内置任务数据
                taskList = new List<Task>(256);
                taskList.Add(Task以牙还牙以眼还眼);
                #endregion

                Save();
            }

            foreach (Task m in taskList)
            {
                taskDict.Add(m.Name, m);
                Console.WriteLine("任务："+m.Name+"\t 去"+m.AddressName+" 杀死"+m.MonsterAmount+"只"+m.MonsterName+"\tCode:"+BitConverter.ToString(m.Code));
            }
            Console.WriteLine("加载任务数据库完成！");
        }

        /// <summary>
        /// 根据名称获取任务
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Task GetTask(string name)
        {
            if (taskDict.ContainsKey(name))
            {
                return taskDict[name];
            }
            else
            {
                Tools.PrintException(new Exception("任务："+name+" 没有输入数据库。"));
                return null;
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public static void Save()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "//data//task.xml";
            try
            {
                using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Task>));
                    serializer.Serialize(stream, taskList);
                    stream.Close();
                }
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("保存任务数据库出错，错误信息：");
                Tools.PrintException(ex);
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public static void Load()
        {
            try
            {
                using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\data\\task.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Task>));
                    taskList = (List<Task>)serializer.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("加载任务数据库出错，已采用程序内置数据，错误信息：");
                Tools.PrintException(ex);
            }
        }
    }
}
